cc.Class({
    extends: cc.Component,

    properties: {
        btnReturn:cc.Button,
        btnAddGlod:cc.Button,
        btnNowUnlock:cc.Button,
        btnJieSuo:cc.Button,
        pageContent:cc.Node,
        nodePage:cc.Node,
        pageArr:[cc.Node],
        pfShopItem:cc.Prefab,
        spBall:cc.Sprite,
        txtGold:cc.Label,
        defaultSkin:null, 
        shopType:null, 
        spriteArr:[cc.spriteFrame],
        pageView:cc.PageView,
        itemArr:[],
        ageinNum:0,
    },

    onLoad () {

        this.txtGold.string = window.gameConfig.gold;
        if(this.shopType == "ball")
        {
            //篮球皮肤界面
            this.defaultSkin = window.gameConfig.getGameConfigData("ballSkin");
            this.onLoadSkinRes("shop/ball/", this.saveAllSkinSpriteFrame, "ball");
            this.onLoadBallSkinSprite("ball/"+ this.defaultSkin);
        }
        else
        {
            //弹杆皮肤界面
            this.defaultSkin = window.gameConfig.getGameConfigData("ganSkin");
            this.onLoadSkinRes("shop/gan/", this.saveAllSkinSpriteFrame,"gan");
            this.onLoadBallSkinSprite("gan/" + this.defaultSkin);
        }
    },

    //保存所有加载出来的图片
    saveAllSkinSpriteFrame:function(frame, self){
        for(let i = 0; i < frame.length; i++)
        {
            if(frame[i].name.indexOf(self.shopType) != -1)
            {
                self.spriteArr.push(frame[i]);
            }
        }

        self.setSkinItemList();
    },

    //生成单个皮肤节点
    setSkinItemList:function(){
        //算出需要几个分页
        let pageNum = Math.ceil(this.spriteArr.length / 9);
        let defaultSkin;
        if(this.shopType == "ball")defaultSkin = window.gameConfig.getGameConfigData("ballSkin");
        else defaultSkin = window.gameConfig.getGameConfigData("ganSkin");

        //生成分页节点
        for(let j = 0; j < pageNum; j++)
        {
            this.pageArr[j].active = true;
        }

        //生成单个皮肤节点
        for(let i = 0; i < this.spriteArr.length; i++)
        {
            let pfSkin = window.creatrPrefab(this.pfShopItem);
            let skinItem = pfSkin.getComponent("shopItem");
            skinItem.setSkinData(this.spriteArr[i], this, defaultSkin, this.shopType);
            this.itemArr.push(skinItem)
            if(i < 9)this.pageArr[0].addChild(pfSkin);
            else if(i > 8 && i < 18)this.pageArr[1].addChild(pfSkin);
            // else if(i >= 18 && i < 27)this.pageArr[2].addChild(pfSkin);
            // else if(i >= 27 && i < 36)this.pageArr[3].addChild(pfSkin);
        }
    },

    setShopType:function(type){
         //篮球皮肤界面
         this.shopType = type;
    },

    onLoadSkinRes:function(url, callback = null){
        var self = this;
        cc.loader.loadResDir("image/"+url,cc.spriteFrame,function (err, spriteFrame) {
            if(err == null)
            {
                if(callback == null)return
                callback(spriteFrame, self);
            }
        });
    },

    //返回
    onBtnReturnCheckCall:function(){
        window.audioMgr.prototype.playButtonAudio();
        removeNodeByParent(this.node)
    },

    //看视频获取钻石
    onBtnAddGlodCheckCall:function(){
        window.audioMgr.prototype.playButtonAudio();
    },

    //立即解锁
    onBtnNowUnlockCheckCall:function(){
        window.audioMgr.prototype.playButtonAudio();
        if(window.gameConfig.gold < 300)return;
        let pageNum = this.pageView.getCurrentPageIndex();
        this.unLockSkin(pageNum);
    },


    unLockSkin:function(number){
        let skinItem = this.pageArr[number].children;
        let ran = Math.floor(Math.random() * 10);
        if(skinItem[ran] == null)
        {
            this.unLockSkin(number);
            return
        }
        let shopItem = skinItem[ran].getComponent("shopItem");
        //判断随机选中的皮肤是否解锁
        let isUnlock = shopItem.isUnlockSkin();
        if(!isUnlock)
        {
            //重新抽取
            if(this.ageinNum >= 9)return;
            this.unLockSkin(number);
            this.ageinNum + 1;
        }
        else
        {
            //解锁皮肤
            shopItem.unlockSkin();
        }
        this.txtGold.string = window.gameConfig.gold;
    },

    //看视频解锁
    onBbtnJieSuoCheckCall:function(){
        window.audioMgr.prototype.playButtonAudio();
    },

    //切换皮肤
    setDefaultBallSkin:function(name){
        this.defaultSkin = name;
        for(let i = 0; i < this.spriteArr.length; i++)
        {
            if(this.spriteArr[i].name == name)
            {
                this.spBall.spriteFrame = this.spriteArr[i];
            }
        }
    },

    //获取当前使用的皮肤
    onLoadBallSkinSprite:function(url){
        window.onLoadSpriteRes("shop/"+url, cc.SpriteFrame, this.setBallSkinCallback, this)
    },

    setBallSkinCallback(spSkin, self){
        if(spSkin == null)return;
        self.spBall.spriteFrame = spSkin;
    },
});
